package com.gitee.wsl.compose.ui.richtext.theme

import androidx.compose.runtime.Immutable
import androidx.compose.ui.text.SpanStyle
import com.gitee.wsl.compose.ui.richtext.content.Format

/**
 * Defines the [SpanStyle]s that are used for various [RichTextString] formatting directives.
 */
@Immutable
 data class RichTextStringStyle(
    val boldStyle: SpanStyle? = null,
    val italicStyle: SpanStyle? = null,
    val underlineStyle: SpanStyle? = null,
    val strikethroughStyle: SpanStyle? = null,
    val subscriptStyle: SpanStyle? = null,
    val superscriptStyle: SpanStyle? = null,
    val codeStyle: SpanStyle? = null,
    val linkStyle: SpanStyle? = null
) {
  internal fun merge(otherStyle: RichTextStringStyle?): RichTextStringStyle {
    if (otherStyle == null) return this
    return RichTextStringStyle(
        boldStyle = boldStyle.merge(otherStyle.boldStyle),
        italicStyle = italicStyle.merge(otherStyle.italicStyle),
        underlineStyle = underlineStyle.merge(otherStyle.underlineStyle),
        strikethroughStyle = strikethroughStyle.merge(otherStyle.strikethroughStyle),
        subscriptStyle = subscriptStyle.merge(otherStyle.subscriptStyle),
        superscriptStyle = superscriptStyle.merge(otherStyle.superscriptStyle),
        codeStyle = codeStyle.merge(otherStyle.codeStyle),
        linkStyle = linkStyle.merge(otherStyle.linkStyle)
    )
  }

  internal fun resolveDefaults(): RichTextStringStyle =
      RichTextStringStyle(
          boldStyle = boldStyle ?: Format.Bold.DefaultStyle,
          italicStyle = italicStyle ?: Format.Italic.DefaultStyle,
          underlineStyle = underlineStyle ?: Format.Underline.DefaultStyle,
          strikethroughStyle = strikethroughStyle ?: Format.Strikethrough.DefaultStyle,
          subscriptStyle = subscriptStyle ?: Format.Subscript.DefaultStyle,
          superscriptStyle = superscriptStyle ?: Format.Superscript.DefaultStyle,
          codeStyle = codeStyle ?: Format.Code.DefaultStyle,
          linkStyle = linkStyle ?: Format.Link.DefaultStyle
      )

   companion object {
     val Default: RichTextStringStyle = RichTextStringStyle()

    private fun SpanStyle?.merge(otherStyle: SpanStyle?): SpanStyle? = this?.merge(otherStyle) ?: otherStyle
  }
}